import config
import webbrowser
import pyperclip
from getwebvpnpath import aget
from PyQt5.QtWidgets import QMainWindow,QMessageBox
from window2_ui import Ui_MainWindow
class window2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.setFixedSize(359, 193)
        self.ui.setupUi(self)

        self.ui.pushButton.clicked.connect(self.button_clicked)
    def button_clicked(self):
        url = self.ui.lineEdit.text()
        ret = aget(url)
        twfid = config.gcon("setting","twfid")
        
        if ret=="错误":
            QMessageBox.warning(self,"注意","输入的网址不正确,请以*://*.*/*的形式输入")
            return
        
        if hasattr(self,"win1"):
            self.win1.detect_vpnlogin()
            try:
                twfid = config.gcon("setting","twfid")
            except Exception:
                QMessageBox.about(self,"提示：","twfid未获取成功，请尝试点击进入http无限流")
                print("twfid未存在")
                return
        ret = f"https://webvpn.stu.edu.cn/portal/shortcut.html?twfid={twfid}&url={ret}"

        
        choice = QMessageBox.question(self,"询问：","是否直接打开解析后网址（若是选否，则复制到剪贴板）",QMessageBox.Yes|QMessageBox.No,QMessageBox.Yes)
        
        if choice == QMessageBox.Yes:
            webbrowser.open(ret)
        else:
            pyperclip.copy(ret)
            #QMessageBox.information(self,"提示","网址已复制到剪贴板")


